package jun.carlauncher.utils;

import android.os.Environment;

import java.io.File;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/09/27
 *     desc  : 清除相关工具类
 * </pre>
 */
public final class CleanUtils {

  private CleanUtils() {
    throw new UnsupportedOperationException("u can't instantiate me...");
  }

  /**
   * 清除内部缓存
   * <p>/data/data/com.xxx.xxx/cache</p>
   *
   * @return {@code true}: 清除成功<br>{@code false}: 清除失败
   */
  public static boolean cleanInternalCache() {
    return deleteFilesInDir(Utils.getApp().getCacheDir());
  }

  /**
   * 清除内部文件
   * <p>/data/data/com.xxx.xxx/files</p>
   *
   * @return {@code true}: 清除成功<br>{@code false}: 清除失败
   */
  public static boolean cleanInternalFiles() {
    return deleteFilesInDir(Utils.getApp().getFilesDir());
  }

  /**
   * 清除内部数据库
   * <p>/data/data/com.xxx.xxx/databases</p>
   *
   * @return {@code true}: 清除成功<br>{@code false}: 清除失败
   */
  public static boolean cleanInternalDbs() {
    return deleteFilesInDir(Utils.getApp().getFilesDir().getParent() + File.separator + "databases");
  }

  /**
   * 根据名称清除数据库
   * <p>/data/data/com.xxx.xxx/databases/dbName</p>
   *
   * @param dbName 数据库名称
   * @return {@code true}: 清除成功<br>{@code false}: 清除失败
   */
  public static boolean cleanInternalDbByName(final String dbName) {
    return Utils.getApp().deleteDatabase(dbName);
  }

  /**
   * 清除内部SP
   * <p>/data/data/com.xxx.xxx/shared_prefs</p>
   *
   * @return {@code true}: 清除成功<br>{@code false}: 清除失败
   */
  public static boolean cleanInternalSP() {
    return deleteFilesInDir(Utils.getApp().getFilesDir().getParent() + File.separator + "shared_prefs");
  }

  /**
   * 清除外部缓存
   * <p>/storage/emulated/0/android/data/com.xxx.xxx/cache</p>
   *
   * @return {@code true}: 清除成功<br>{@code false}: 清除失败
   */
  public static boolean cleanExternalCache() {
    return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && deleteFilesInDir(Utils.getApp().getExternalCacheDir());
  }

  /**
   * 清除自定义目录下的文件
   *
   * @param dirPath 目录路径
   * @return {@code true}: 清除成功<br>{@code false}: 清除失败
   */
  public static boolean cleanCustomCache(final String dirPath) {
    return deleteFilesInDir(dirPath);
  }

  /**
   * 清除自定义目录下的文件
   *
   * @param dir 目录
   * @return {@code true}: 清除成功<br>{@code false}: 清除失败
   */
  public static boolean cleanCustomCache(final File dir) {
    return deleteFilesInDir(dir);
  }

  public static boolean deleteFilesInDir(final String dirPath) {
    return deleteFilesInDir(getFileByPath(dirPath));
  }

  private static boolean deleteFilesInDir(final File dir) {
    if (dir == null) return false;
    // 目录不存在返回true
    if (!dir.exists()) return true;
    // 不是目录返回false
    if (!dir.isDirectory()) return false;
    // 现在文件存在且是文件夹
    File[] files = dir.listFiles();
    if (files != null && files.length != 0) {
      for (File file : files) {
        if (file.isFile()) {
          if (!file.delete()) return false;
        } else if (file.isDirectory()) {
          if (!deleteDir(file)) return false;
        }
      }
    }
    return true;
  }

  private static boolean deleteDir(final File dir) {
    if (dir == null) return false;
    // 目录不存在返回true
    if (!dir.exists()) return true;
    // 不是目录返回false
    if (!dir.isDirectory()) return false;
    // 现在文件存在且是文件夹
    File[] files = dir.listFiles();
    if (files != null && files.length != 0) {
      for (File file : files) {
        if (file.isFile()) {
          if (!file.delete()) return false;
        } else if (file.isDirectory()) {
          if (!deleteDir(file)) return false;
        }
      }
    }
    return dir.delete();
  }

  private static File getFileByPath(final String filePath) {
    return isSpace(filePath) ? null : new File(filePath);
  }

  private static boolean isSpace(final String s) {
    if (s == null) return true;
    for (int i = 0, len = s.length(); i < len; ++i) {
      if (!Character.isWhitespace(s.charAt(i))) {
        return false;
      }
    }
    return true;
  }
}